package no.hig.imt4721.template;

import java.util.ArrayList;
import java.util.List;

import no.hig.imt4721.model.Session;
import no.hig.imt4721.model.Subject;
import no.hig.imt4721.model.Template;

public abstract class TemplateCreator
{	
	private List<Subject> templateData = new ArrayList<Subject>();
	private List<Subject> inputData = new ArrayList<Subject>();
	
	protected List<Template> templates = new ArrayList<Template>();
	
	protected int repetitionsForTemplate = 0;
	protected int sessionsForTemplate = 0;
	protected int sessionsToDisregard = 0;
	
	private static int NUMBER_OF_SESSIONS = 8;
	private static int NUMBER_OF_REPETITIONS = 50;
	
	public TemplateCreator(int sessionsToDisregard, int sessionsForTemplate, int repetitionsForTemplate)
	{
		this.sessionsToDisregard = sessionsToDisregard;
		this.sessionsForTemplate = sessionsForTemplate;
		this.repetitionsForTemplate = repetitionsForTemplate;
	}
	
	protected void splitData(List<Subject> subjects)
	{
		// Divide data up in data for template and data for input
		for(int subjectNumber = 0; subjectNumber < subjects.size(); subjectNumber++)
		{
			Subject templateDataSubject = new Subject();
			Subject inputSubject = new Subject();
			
			templateDataSubject.setName(subjects.get(subjectNumber).getName());
			inputSubject.setName(subjects.get(subjectNumber).getName());
			
			List<Session> templateDataSessions = new ArrayList<Session>();
			List<Session> inputDataSessions = new ArrayList<Session>();
			
			for(int sessionNumber = 0; sessionNumber < NUMBER_OF_SESSIONS; sessionNumber++)
			{
				// Disregard some sessions
				if(sessionNumber < sessionsToDisregard)
					continue;
				
				// We do not use the session in the template
				if(sessionNumber >= sessionsForTemplate)
				{
					Session inputSession = new Session();
					
					for(int repetitionNumber = 0; repetitionNumber < NUMBER_OF_REPETITIONS; repetitionNumber++)
					{
						inputSession.addRepetition(subjects.get(subjectNumber).getSessions().get(sessionNumber).getRepetitions().get(repetitionNumber));
					}
					
					inputDataSessions.add(inputSession);
				}
				else // We use the session in the template
				{
					Session templateSession = new Session();
					Session inputSession = new Session();
					
					for(int repetitionNumber = 0; repetitionNumber < NUMBER_OF_REPETITIONS; repetitionNumber++)
					{
						if(repetitionNumber >= repetitionsForTemplate) // We want to use the repetition for the input
							inputSession.addRepetition(subjects.get(subjectNumber).getSessions().get(sessionNumber).getRepetitions().get(repetitionNumber));
						else // We want to use the repetition for the template
							templateSession.addRepetition(subjects.get(subjectNumber).getSessions().get(sessionNumber).getRepetitions().get(repetitionNumber));
					}
					
					if(templateSession.getRepetitions().size() > 0)
						templateDataSessions.add(templateSession);

					if(inputSession.getRepetitions().size() > 0)
						inputDataSessions.add(inputSession);
				}
			}
			
			templateDataSubject.setSessions(templateDataSessions);
			inputSubject.setSessions(inputDataSessions);
			
			templateData.add(templateDataSubject);
			inputData.add(inputSubject);
		}
	}
	
	
	public List<Template> getTemplates()
	{
		return templates;
	}

	public List<Subject> getInputs()
	{
		return inputData;
	}
	
	public List<Subject> getTemplateData()
	{
		return templateData;
	}

	public abstract void calculate(List<Subject> subjects);
}
